<?php
namespace app\index\controller;

use app\common\model\Chat;
use app\common\model\Info;
use app\common\model\Qqjob;
use app\common\model\Qqs;
use app\common\model\Gg;
use app\common\model\Users;
use app\common\model\Weblist;
use app\common\model\Prizes;
use app\common\model\Prizelog;
use app\common\model\Qds;
use app\common\model\Jfdh;

class Pc extends Common
{
	public function _empty()
    {
        exit($this->fetch('pc/common/empty'));
    }
	
	function __construct()
    {
        parent:: __construct();
        //检测用户VIP是否过期
        if (strtotime($this->userInfo['viptime']) < time()) {
            Users::where('uid', '=', $this->userInfo['uid'])->update(['vipstart' => NULL, 'viptime' => NULL]);
        }
        $this->qqInfo['count'] = Qqs::getMyQqCount($this->userInfo['uid']);
        $this->assign('qqInfo', $this->qqInfo);
		
    }
	
    public function index($act='user',$s='index')
    {
		switch ($act) {
			case 'user':
				switch ($s) {
					case 'index':
						$this->siteCount['user'] = Users::where('web_id', '=', WEB_ID)->count();
						$this->siteCount['qq'] = Qqs::where('web_id', '=', WEB_ID)->count();
						$this->siteCount['mzcount'] = Qqjob::allSiteMzNum();
						$this->siteCount['mpcount'] = Qqjob::allSiteMpNum();
						$chatList = Chat::order('id desc')->limit(10)->select();
						if(is_daili($this->userInfo['dlpower'])!=='普通用户'){
							$is_daili = 1;
						}else{
							$is_daili = 0;
						}
						$this->assign([
							'siteCount'	=>	$this->siteCount,
							'web_id'	=>	WEB_ID,
							'is_daili'	=>	$is_daili,
							'webTitle'	=>	'用户中心'
						]);
						return $this->fetch('pc/user/index');
					break;
					case 'qqlist':
						$this->assign('webTitle', 'QQ管理');
						return $this->fetch('pc/user/qqlist');
					break;
					case 'addqq':
						if(!input('get.qid')){
							$this->assign('row', '');
							$this->assign('webTitle', '添加QQ');
						}else{
							$this->assign('row', Qqs::findByqid(input('get.qid')));
							$this->assign('webTitle', '更新QQ');
						}
						echo $this->fetch('pc/user/addqq');
						break;
					case 'saoma':
						if(!input('get.qid')){
							$this->assign('row', '');
							$this->assign('webTitle', '扫码添加QQ');
						}else{
							$this->assign('row', Qqs::findByqid(input('get.qid')));
							$this->assign('webTitle', '扫码更新QQ');
						}
						echo $this->fetch('pc/user/saoma');
						break;
					case 'qqset':
						$page = input('get.page');
						$pageSize = 15;
						$page = ($page < 1) ? 1 : $page;
						$pageSize = ($pageSize < 1 || $pageSize > 50) ? 10 : $pageSize;
						$qid = input('get.qid');
						$qq_data = Qqs::where('qid', '=', $qid)
							->where('uid', '=', $this->userInfo['uid'])
							->find();
						$whrer = [
							'uid'	=> $this->userInfo['uid']
						];
						$qqList = Qqs::getMyQqList($this->userInfo['uid'],$page,$pageSize);
						$QQlist='';
						foreach ($qqList['list'] as $row) {
							if($row['qid']==$qq_data['qid']){
								$active='active';
							}else{
								$active='';
							}
							
							$QQlist .= '<li class="'.$active.'"> <a class="t-v" href="'.url('pc/index',['act'=>'user','s'=>'qqset']).'?qid='.$row['qid'].'" data-pjax=""> 
							 <div style="width:35px;">
							  <img class="w6 h6 b r5" src="//q4.qlogo.cn/headimg_dl?dst_uin='.$row['qq'].'&amp;spec=40" />
							 </div> 
							 <div>
							  <font class="v-1">'.$row['qq'].'</font>
							 </div> </a> </li> ';
						}
						$this->assign([
							'row'	=>	$qq_data,
							'webTitle'	=>	'QQ详细功能设置',
							'qqlist'	=>	$QQlist
						]);
						echo $this->fetch('pc/user/qqset');
					break;
					case 'gnlist':
						$uin = input('qid');
						$data = gnlist();
						$qqinfo = model('Qqs')->where('qid', '=', $uin)->find();
						$html='';
						foreach($data as $k=>$v){
							if($data[$k]['vip']==0){
								$is_vip = '<font class="f3 lv">免费</font>';
							}elseif($data[$k]['vip']==1){
								$is_vip = '<font class="f3 hong">VIP</font>';
							}else{
								$is_vip = '';
							}
							if (!Qqjob::findByType($data[$k]['id'], $qqinfo['qq'])) {
								$html .='<dl><a class="in p4" href="javascript:;" onclick="x.ajax(\''.url('pc_ajax/gnset',['act'=>'add']).'\',\'type='.$data[$k]['id'].'&amp;qid='.$qqinfo['qid'].'&amp;uin='.$qqinfo['qq'].'&amp;gnid='.$data[$k]['gnid'].'\',1);"><dt><img class="w7 h7 ml2 mr4" src="/Static/img/is'.$data[$k]['id'].'.png"></dt><dd><p class="pr5">添加 '.$data[$k]['name'].' 功能<span class="ml4">['.$is_vip.']</span></p></dd><dt><span class="pr3 f3 lv '.$data[$k]['id'].'access"></span></dt><dt><i class="icon icon-pright hui"></i></dt></a></dl>';
							}else{
								$html .='
								<dl><a class="in p4" href="javascript:;" onclick="x.msg(\'此功能已经添加在列表中\');">
								<dt><img class="w7 h7 ml2 mr4" src="/Static/img/is'.$data[$k]['id'].'.png"></dt>
								<dd><p class="pr5">添加 '.$data[$k]['name'].' 功能<span class="ml4">['.$is_vip.']</span></p></dd>
								<dt><span class="pr3 f3 lv '.$data[$k]['id'].'ccess">已添加</span></dt>
								<dt><i class="icon icon-pright hui"></i></dt>
								</a></dl>';
							}
						}
						$this->assign('html', $html);
						echo $this->fetch('/pc/html/gnlist');
						break;
					default:
					$this->_empty();
				}
			break;
			case 'shop':
				switch ($s) {
					case 'vip':
						$this->assign('webTitle', '开通VIP会员');
						return $this->fetch('/pc/shop/vip');
					break;
					case 'peie':
						$this->assign('webTitle', '购买挂机配额');
						return $this->fetch('/pc/shop/peie');
					break;
					case 'rmb':
						$this->assign('webTitle', '充值账户余额');
						return $this->fetch('/pc/shop/rmb');
					break;
					case 'daili':
						$this->assign('webTitle', '开通平台代理');
						return $this->fetch('/pc/shop/daili');
					break;
					case 'fz':
						$fzurls = explode(',', config('sys_fz_url'));
						$this->assign([
							'fzurls'	=>	$fzurls,
							'webTitle'	=>	'搭建高级主站'
						]);
						return $this->fetch('/pc/shop/fz');
					break;
					default:
					$this->_empty();
				}
			break;
			case 'other':
				switch ($s) {
					case 'qd':
						switch (config('sys_sqd_give_type')) {
							case 1:
								$shop = '' . config('sys_sqd_shop_num') . '天VIP会员';
								break;
							case 2:
								$shop = '' . config('sys_sqd_shop_num') . '个配额';
								break;
							case 3:
								$shop = '' . config('sys_sqd_shop_num') . '元余额';
								break;
							default:
							$shop = '站长还没有配置签到奖品';
						}
						if (config('sys_qd_user_give_jf') == '') {
							$user_jf = '0,0';
						} else {
							$user_jf = config('sys_qd_user_give_jf');
						}
						$data = [
							'user_jf' => explode(",", $user_jf),
							'give_shop' => $shop,
							'day' => Qds::findDayByUid($this->userInfo['uid'])
						];
						$this->assign('qd', $data);
						$this->assign('webTitle', '每日签到');
						return $this->fetch('/pc/other/qd');
					break;
					case 'jfdh':
						$this->assign('webTitle', '积分兑换');
						$this->assign('dlname', is_Daili(config('sys_jfdh_daili_num')));
						return $this->fetch('/pc/other/jfdh');
					break;
					case 'jfcj':
						$list = Prizes::order('addtime desc')
						->select();
						$jplist='';
						foreach ($list as $row) {
							$jplist .= '<span class="tag bg-qing">' . $row['name'] . '</span>
							';
						}
						$this->assign('jplist', $jplist);
						$this->assign('webTitle', '积分抽奖');
						return $this->fetch('/pc/other/jfcj');
					break;
					default:
					$this->_empty();
				}
			break;
			case 'link':
				switch ($s) {
					case 'dlyz':
						$this->assign('webTitle', '代理验证');
						return $this->fetch('/pc/link/daili');
					break;
					case 'mzrz':
						$this->assign('webTitle', '秒赞认证');
						return $this->fetch('/pc/link/mzrz');
					break;
					case 'mzq':
						$this->assign('webTitle', 'QQ秒赞墙');
						return $this->fetch('/pc/link/mzq');
					break;
					default:
					$this->_empty();
				}
			break;
			case 'daili':
				switch ($s) {
					case 'index':
						if(is_daili($this->userInfo['dlpower'])=='普通用户'){
							$this->_empty();
						}
						$this->assign('webTitle', '代理后台');
						return $this->fetch('pc/daili/index');
					break;
					default:
					$this->_empty();
				}
			break;
			case 'set':
				switch ($s) {
					case 'nickname':
						$this->assign('webTitle', '修改昵称');
						return $this->fetch('/pc/set/nickname');
					break;
					case 'pass':
						$this->assign('webTitle', '修改密码');
						return $this->fetch('/pc/set/pass');
					break;
					case 'qq':
						$this->assign('webTitle', '换绑QQ');
						return $this->fetch('/pc/set/qq');
					break;
					default:
					$this->_empty();
				}
			break;
			default:
			$this->_empty();
		}
    }
	
	public function admin($act='set',$s='info')
    {
		if(config('web_user_id')!==$this->userInfo['uid']){
			$this->_empty();
		}
		switch ($act) {
			case 'set':
				switch ($s) {
					case 'info':
						$web_data = Weblist::
						 where('web_id', '=', $this->userInfo['web_id'])
						->find();
						$this->assign('webdata', $web_data);
						$this->assign('webTitle', '网站信息');
						return $this->fetch('pc/admin/index');
					break;
					case 'reg':
						$this->assign('webTitle', '注册奖励');
						return $this->fetch('pc/admin/set/reg');
					break;
					case 'mail':
						$this->assign('webTitle', '邮箱提醒');
						return $this->fetch('pc/admin/set/mail');
					break;
					case 'chat':
						$this->assign('webTitle', '聊天发言');
						return $this->fetch('pc/admin/set/chat');
					break;
					case 'task':
						$this->assign('webTitle', '任务运行');
						if(WEB_ID!==1){
							$this->_empty();
						}
						return $this->fetch('pc/admin/set/task');
					break;
					case 'cron':
						$this->assign('webTitle', '任务监控');
						if(WEB_ID!==1){
							$this->_empty();
						}
						return $this->fetch('pc/admin/set/cron');
					break;
					case 'plugins':
						$this->assign('webTitle', '网站插件');
						return $this->fetch('pc/admin/set/plugins');
					break;
					case 'server':
						$this->assign('webTitle', '服务器设置');
						if(WEB_ID!==1){
							$this->_empty();
						}
						return $this->fetch('pc/admin/set/server');
					break;
					default:
					$this->_empty();
				}
			break;
			case 'pay':
				switch ($s) {
					case 'e':
						$this->assign('webTitle', '支付配置');
						return $this->fetch('pc/admin/pay/e');
					break;
					case 'o':
						$this->assign('webTitle', '订单列表');
						return $this->fetch('pc/admin/pay/o');
					break;
					case 'vip':
						$this->assign('webTitle', 'VIP价格');
						return $this->fetch('pc/admin/pay/vip');
						break;
					case 'daili':
						$this->assign('webTitle', '代理价格');
						return $this->fetch('pc/admin/pay/daili');
						break;
					case 'peie':
						$this->assign('webTitle', '配额价格');
						return $this->fetch('pc/admin/pay/peie');
						break;
						break;
					case 'fz':
						$this->assign('webTitle', '分站价格');
						return $this->fetch('pc/admin/pay/fz');
						break;
					default:
					$this->_empty();
				}
			break;
			case 'data':
				switch ($s) {
					case 'user_list':
						$this->assign('webTitle', '用户列表');
						return $this->fetch('pc/admin/data/user_list');
					break;
					case 'qq_list':
						$this->assign('webTitle', 'QQ列表');
						return $this->fetch('pc/admin/data/qq_list');
					break;
					case 'gg_list':
						$this->assign('webTitle', '公告列表');
						return $this->fetch('pc/admin/data/gg_list');
					break;
					case 'fz_list':
						$this->assign('webTitle', '分站列表');
						return $this->fetch('pc/admin/data/fz_list');
					break;
					default:
					$this->_empty();
				}
			case 'edit':
				switch ($s) {
					case 'user':
						$id = intval(input('get.uid'));
						$res = Users::where('uid', '=', $id)->find();
						if(!$res){
							$this->_empty();
						}
						$this->assign('data', $res);
						$this->assign('webTitle', '编辑用户');
						return $this->fetch('pc/admin/data/edit/user');
					break;
					case 'gg':
						$id = intval(input('get.id'));
						$res = Gg::where('id', '=', $id)->find();
						if(!$res){
							$this->_empty();
						}
						$this->assign('data', $res);
						$this->assign('webTitle', '编辑公告');
						return $this->fetch('pc/admin/data/edit/gg');
					break;
					case 'fz':
						$id = intval(input('get.web_id'));
						$res = Weblist::where('web_id', '=', $id)->find();
						if(!$res){
							$this->_empty();
						}
						$this->assign('data', $res);
						$this->assign('webTitle', '编辑分站');
						return $this->fetch('pc/admin/data/edit/fz');
					break;
					default:
					$this->_empty();
				}
			break;
			case 'add':
				switch ($s) {
					case 'gg':
						$this->assign('webTitle', '添加公告');
						return $this->fetch('pc/admin/data/add/gg');
					break;
					case 'fz':
						$this->assign('webTitle', '添加分站');
						return $this->fetch('pc/admin/data/add/fz');
					break;
					default:
					$this->_empty();
				}
			break;
			case 'other':
				switch ($s) {
					case 'jfdh':
						$this->assign('webTitle', '积分兑换设置');
						return $this->fetch('pc/admin/other/jfdh');
						break;
					case 'qd':
						$this->assign('webTitle', '每日签到设置');
						return $this->fetch('pc/admin/other/qd');
						break;
					case 'cj':
						$this->assign('webTitle', '抽奖商品设置');
						return $this->fetch('pc/admin/other/cj');
						break;
					case 'cjset':
						$this->assign('webTitle', '抽奖规则设置');
						return $this->fetch('pc/admin/other/cjset');
						break;
					default:
					$this->_empty();
				}
			break;
			default:
			$this->_empty();
		}
    }
	
	protected
    function _initialize()
    {
        parent::_initialize();
        $this->checkLogin();
		if(check_mobile()){
			header('Location: '.url('mobile/index').'');
			exit;
		}
		
    }
	
	private
    function checkLogin()
    {
        $check_Login = Users::checkLogin();
        if ($check_Login != null) {
            $this->assign('alert', $check_Login['msg']);
            $this->assign('url', $check_Login['url']);
            exit($this->fetch('pc/common/msg'));
        } else {
            $this->userInfo = Users::getLoginUser();
            $this->assign('userInfo', $this->userInfo);

        }
    }
}
